package com.ebraga.trackerclub.android.handler.core;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ebraga.trackerclub.android.dao.entity.RankingAndroid;
import com.ebraga.trackerclub.android.exception.BackofficeDBConnectionException;
import com.ebraga.trackerclub.android.exception.handler.BackofficeHandlerException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeIllegalArgumentManagerException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeInvalidSessionManagerException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeManagerException;
import com.ebraga.trackerclub.android.handler.api.RankingHandler;
import com.ebraga.trackerclub.android.manager.api.RankingManager;
import com.ebraga.trackerclub.android.util.DLog;
import com.ebraga.trackerclub.android.vo.RankingVO;

public abstract class RankingHandlerImpl implements RankingHandler {
	
	protected RankingManager rankingManager;

	@Override
	public void insert(RankingVO ranking) throws BackofficeHandlerException {
		
		if (ranking == null){
			throw new BackofficeHandlerException("Posição vazia"); 
		}
		
		if (ranking.getName() == null){
			throw new BackofficeHandlerException("Usuario não informado para a posição"); 
		}
		try {
			RankingAndroid rankingAndroid = parseRankingVOToEntity (ranking);
		
			rankingManager.insert(rankingAndroid);
		}catch(BackofficeIllegalArgumentManagerException i){
			DLog.e(i);
			throw new BackofficeHandlerException("Argumentos Usuario ou Posição invalidos"); 
		}catch(BackofficeDBConnectionException db){
			DLog.e(db);
			throw new BackofficeHandlerException("Erro ao conectar Banco de Dados");
		}catch(BackofficeInvalidSessionManagerException db){
			DLog.e(db);
			throw new BackofficeHandlerException("Erro no Contexto Android");
		}catch(BackofficeManagerException me){
			DLog.e(me);
			throw new BackofficeHandlerException("Erro ao inserir Posição");
 		}
	}
	
	public List<RankingVO> retrieveRanking() throws BackofficeHandlerException {
		
		try{
			List<RankingAndroid> rankingAndroids = null;
			List<RankingVO> rankingVOs = new ArrayList<RankingVO>();
			
			rankingAndroids = rankingManager.retrieveRanking();
			
			
			RankingVO rankingVO;
			for (RankingAndroid rankingAndroid : rankingAndroids) {
				rankingVO = parseEntityToRankingVO (rankingAndroid);
				rankingVOs.add(rankingVO);
			}
			
			return rankingVOs;
		}catch(BackofficeIllegalArgumentManagerException i){
			DLog.e(i);
			throw new BackofficeHandlerException("Argumentos Usuario ou Posição invalidos"); 
		}catch(BackofficeDBConnectionException db){
			DLog.e(db);
			throw new BackofficeHandlerException("Erro ao conectar Banco de Dados");
		}catch(BackofficeInvalidSessionManagerException db){
			DLog.e(db);
			throw new BackofficeHandlerException("Erro no Contexto Android");
		}catch(BackofficeManagerException me){
			DLog.e(me);
			throw new BackofficeHandlerException("Erro ao inserir Posição");
 		}		
		
	}
	

	public void deleteAllLines() throws BackofficeHandlerException {
		
		try{
			
			rankingManager.deleteAllLines();
			
		}catch(BackofficeIllegalArgumentManagerException i){
			DLog.e(i);
			throw new BackofficeHandlerException("Argumentos Usuario ou Posição invalidos"); 
		}catch(BackofficeDBConnectionException db){
			DLog.e(db);
			throw new BackofficeHandlerException("Erro ao conectar Banco de Dados");
		}catch(BackofficeInvalidSessionManagerException db){
			DLog.e(db);
			throw new BackofficeHandlerException("Erro no Contexto Android");
		}catch(BackofficeManagerException me){
			DLog.e(me);
			throw new BackofficeHandlerException("Erro ao inserir Posição");
 		}		
		
	}	
		
	private RankingVO parseEntityToRankingVO(RankingAndroid rankingAndroid) {
		RankingVO rankingVO = new RankingVO();
		
		rankingVO.setBrand(rankingAndroid.getBrand());
		rankingVO.setLatitude(String.valueOf(rankingAndroid.getLatitude()));
		rankingVO.setLongitude(String.valueOf(rankingAndroid.getLongitude()));
		rankingVO.setModel(rankingAndroid.getModel());
		rankingVO.setName(rankingAndroid.getName());
		rankingVO.setSpeed(String.valueOf(rankingAndroid.getSpeed().intValue()));
		rankingVO.setDate(new Date(rankingAndroid.getDate()));		
		
		return rankingVO;
	}

	public RankingAndroid parseRankingVOToEntity (RankingVO rankingVO){
		RankingAndroid rankingAndroid = new RankingAndroid();
		
		rankingAndroid.setBrand(rankingVO.getBrand());
		rankingAndroid.setLatitude(Double.parseDouble(rankingVO.getLatitude()));
		rankingAndroid.setLongitude(Double.parseDouble(rankingVO.getLongitude()));
		rankingAndroid.setModel(rankingVO.getModel());
		rankingAndroid.setName(rankingVO.getName());
		rankingAndroid.setSpeed(Double.parseDouble(rankingVO.getSpeed()));
		rankingAndroid.setDate(rankingVO.getDate().getTime());
		
		return rankingAndroid;
	}
	
	protected void setRankingManager(RankingManager rankingManager) {
		this.rankingManager = rankingManager;
	}
	

}
